//
//  CLstrollViewController.m
//  I-want-go
//
//  Created by dllo on 15/10/21.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CLstrollViewController.h"
#import "CLstrollTableCell.h"
#import "CLtool.h"
#import "BaseModel.h"
#import "CLtitlePictrue.h"
#import "CLunderTableCell.h"
#import "CLunder.h"
#import "CLstrollTableCell.h"
#import "CLclassifyViewController.h"
#import "CLinterViewController.h"
#import "CLseekViewController.h"
#import "CLscreen.h"
@interface CLstrollViewController ()<UITableViewDataSource, UITableViewDelegate, CLstrollTableCellDelegate, UISearchBarDelegate>

@property(nonatomic, retain)UITableView *myTableView;

@property(nonatomic, retain)NSMutableArray *arr;

@property(nonatomic, retain)NSMutableArray *liArr;

@property(nonatomic, assign)NSInteger page;
@property(nonatomic, retain)NSString *str;
@property(nonatomic, retain)UISearchBar *search;

@end

@implementation CLstrollViewController
- (void)dealloc {
    [_str release];
    [_search release];
    [_liArr release];
    [_myTableView release];
    [_arr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor grayColor];
    //
    
    self.navigationController.navigationBar.translucent = NO;
    
    ///////
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375 * SWIDTH, 554 * SHEIGHT)];
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    
    self.myTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
    
    self.search = [[UISearchBar alloc] initWithFrame:CGRectMake(80 * SWIDTH, 20 * SHEIGHT, 200 * SWIDTH, 0)];
    [self.navigationController.navigationBar addSubview:self.search];
    self.search.placeholder = @"搜索物品";
    [self.search setShowsCancelButton:YES];
    
    [self.search release];
    self.search.delegate = self;

    
    [self createData];
    
    [self createMata];
    
}

- (void)footerAction {
    
    self.page = 0;
    self.page++;
    NSString *str = @"http://api.xiangqu.com/search/list";
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *bodyStr =[NSString stringWithFormat: @"key=d5d8ff390a807ba8513f21c45f4fb5cb&page=%ld&t=1445862694849&userid=705811", self.page];
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        NSMutableArray *arArr = [NSMutableArray array];
        arArr = [CLunder baseModelByArr:dic[@"data"][@"records"]];
        for (CLunder *under in arArr) {
            [self.liArr addObject:under];
        }
        [self.liArr removeLastObject];
        [self.myTableView.footer endRefreshing];
        [self.myTableView reloadData];
        
    }];
}

- (void)createData {
    NSString *str = @"http://api.xiangqu.com/category/out/children";
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *bodyStr = @"key=e1829c7ad984809a9290e1a3a3b1f4ff&t=1445821154795";
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        self.arr = [NSMutableArray array];
        self.arr = [CLtitlePictrue baseModelByArr:dic[@"data"]];
        [self.myTableView reloadData];
        
    }];
}


- (void)createMata {
    NSString *str = @"http://api.xiangqu.com/search/list";
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *bodyStr = @"key=d5d8ff390a807ba8513f21c45f4fb5cb&page=0&t=1445862694849&userid=705811";
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        self.liArr = [NSMutableArray array];
        self.liArr = [CLunder baseModelByArr:dic[@"data"][@"records"]];
        
        [self.myTableView reloadData];
        
        
      }];
}



// tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return self.liArr.count;
    }
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        self.myTableView.rowHeight = 250 * SHEIGHT;
        static NSString *reuse = @"reuse";
        CLstrollTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[CLstrollTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
        }
        cell.delate = self;
        
        // 传值
        cell.titArr = self.arr;
        [cell.myCollectionView reloadData];
        //    无色
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;

        return cell;

    }
    else {
        self.myTableView.rowHeight = 180 * SHEIGHT;
        static NSString *reuse = @"raa";
        CLunderTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[CLunderTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
        }
        CLunder *unde = self.liArr[indexPath.row];
        [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:unde.image] placeholderImage:[UIImage imageNamed:@"good.png"]];
        
            cell.leftAddlabel.text = unde.brand;
            cell.leftproducrLabel.text = unde.productDescription;
        //    无色
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;

        return cell;
    }
}

- (void)pushindexid:(NSString *)indexid Withname:(NSString *)name {
    CLclassifyViewController *clasVC = [[CLclassifyViewController alloc] init];
    [self.navigationController pushViewController:clasVC animated:YES];
    clasVC.strId = indexid;
    clasVC.titl = name;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return 40 * SHEIGHT;
    } else {
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width * SWIDTH, 50 * SHEIGHT)];
    headerView.backgroundColor = [UIColor whiteColor];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = CGRectMake(130 * SWIDTH, 0, 100 * SWIDTH, 40 * SHEIGHT);
    leftButton.backgroundColor = [UIColor whiteColor];
    [leftButton setTitle:@"精          选" forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [headerView addSubview:leftButton];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(50 * SWIDTH, 20 * SHEIGHT, 70 * SWIDTH, 1 * SHEIGHT)];
    leftView.backgroundColor = [UIColor blackColor];
    [headerView addSubview:leftView];
    [leftView release];
    
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(240 * SWIDTH, 20 * SHEIGHT, 70 * SWIDTH, 1 * SHEIGHT)];
    rightView.backgroundColor = [UIColor blackColor];
    [headerView addSubview:rightView];
    [rightView release];

    return headerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CLinterViewController *inteVC = [[CLinterViewController alloc] init];
    [self.navigationController pushViewController:inteVC animated:YES];
    
    CLunder *un = self.liArr[indexPath.row];
    NSString *st = [NSString stringWithFormat:@"%@", un.productId];
    
    inteVC.interId = st;
}


// 点取消
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    
    [self.search resignFirstResponder];
}


- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    return YES;
}

//结束编辑
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    NSLog(@"%@", searchBar.text);
}
//开始编辑
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    // 转码 把汉字转换成编码
   
  }
// 实时搜索
- (void)searchBar:(UISearchBar *)searchBar textDidChange:
(NSString *)searchText {
    
}
// 点确定 点搜索走它
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
     self.str = [searchBar.text  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    NSLog(@"%@", self.str);
    NSLog(@"%@", self.str);
    
    CLseekViewController *seekVC = [[CLseekViewController alloc] init];
    [self.navigationController pushViewController:seekVC animated:YES];
        seekVC.searchText = searchBar.text;
    seekVC.str = self.str;
    [self.search resignFirstResponder];

}



















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
